package top.timestreet.xmsk.qu.processor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import top.timestreet.xmsk.base.pojo.ListPage;
import top.timestreet.xmsk.base.processor.BaseSite;
import top.timestreet.xmsk.util.*;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author TateLiao
 * @data 2019年6月2日 笔趣阁(https://www.qu.la/)章节列表抓取 egPage:
 * https://www.qu.la/book/101104/
 */
@Service
public class QuListPageProcessor extends BaseSite implements PageProcessor {

    @Autowired
    private Environment environment;

    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        String baseURL = environment.getProperty("qu.homeURL");
        ListPage listPage = new ListPage();
        // 抓取小说元信息
        listPage.setName(html.$("#info h1", "text").toString());
        listPage.setAuthor(ChapterMeta.metaFormat(html.$("#info p:nth-child(2)", "text").toString()));
        listPage.setLastUpdateTime(ChapterLastUpdateTimeUtil
                .quFormat(ChapterMeta.metaFormat(html.$("#info p:nth-child(4)", "text").toString())));
        // 小说章节
        ArrayList<Map<String, String>> chapters = new ArrayList<Map<String, String>>();
        Map<String, String> chapter = new LinkedHashMap<>();
        List<Selectable> nodes = html.$("#list dd").nodes();
        for (int i = 12; i < nodes.size(); i++) {
            Selectable selectable = nodes.get(i);
            String chapterName = selectable.$("a", "text").toString();
            String chapterUrl = selectable.$("a", "href").toString();
            // 判断是否标题是否包含中文数字
            if (ChapterNameUtil.hasDigit(chapterName) && ChapterNameUtil.chapterNameIsContriner(chapterName)) {
                chapterName = ChapterNameUtil.chapterNameFormat(chapterName);
            }
            chapter.put(chapterName, URLConvertUtil.uRLConvert(DESUtil.encrypt(baseURL + chapterUrl)));
        }
        chapters.add(chapter);
        listPage.setChapterList(chapters);
        page.putField("listPage", listPage);
    }

    @Override
    public Site getSite() {
        return site;
    }

}
